/**
 * Created by TomSean on 2017/11/27.
 */
var BatchRenewalManager = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
BatchRenewalManager.prototype = {
    init : function () {
        var ownerList = ownerManager.ownerList;
        var dataListBody = this.$parent.find('#data-list tbody');
        for (var i = 0; i < ownerList.length; i++) {
            var tr = $('<tr></tr>');
            tr.append('<td>'+ownerList[i].organization.organizationName + '('+ownerList[i].organization.organizationCode+')</td>');
            tr.append('<td data-value="'+ownerList[i].ownerCode+'">'+ownerList[i].ownerName+'('+ownerList[i].ownerCode+')</td>');
            tr.append('<td>'+ownerList[i].vehicleNos+'</td>');
            if (!isEmpty(ownerList[i].spaceList)&&ownerList[i].spaceList.length > 0) {
                var spaceStr = '';
                for (var j = 0; j < ownerList[i].spaceList.length; j++) {
                    spaceStr += '*'+ownerList[i].spaceList[j].spaceCode + '('+ownerList[i].spaceList[j].endTime+')<br>';
                }
                tr.append('<td>'+spaceStr+'</td>');
            } else {
                tr.append('<td>暂无车位</td>');
            }
            tr.append('<td><a href="javascript:void(0)" onclick="batchRenewal.removeThisTr(this)">删除</a></td>');
            dataListBody.append(tr);
        }
    },
    removeThisTr : function (a) {
        $(a).parents('tr:eq(0)').remove();
    },
    prev : function () {
        var $this = this;
        $this.$parent.find('#data-list').fadeIn('fast');
        $this.$parent.find('#calc-page').fadeOut('fast');
    },
    createOrder : function (a) {
        var $this = this;
        var month = this.$parent.find('#month').val();
        var dataList = [];
        this.$parent.find('#data-list table tbody tr:visible').each(function(inx, itm) {
            var ownerCode = $(itm).find('td:eq(1)').attr('data-value');
            dataList.push(ownerCode);
        });
        var dataT = {
            'month':month,
            'list':dataList
        };
        JsonpAjax(baseApiPath + 'owner_info/create_order', JSON.stringify(dataT), 'json', function (res) {
            if (isEmpty(res)) {
                notification({'title':'提醒', 'message':'没有可创建的批量缴费账单！'}, 'error');
                return;
            }
            $this.$parent.find('#data-list').fadeOut('fast');
            $this.$parent.find('#calc-page').fadeIn('fast');
            var tbody = $this.$parent.find('#calc-page table tbody');
            tbody.html('');
            var total = 0;
            var list = res;
            for (var i = 0; i < list.length; i++) {
                var tr = $('<tr></tr>');
                tr.append('<td>'+list[i].ownerName+ '(' + list[i].ownerCode +')</td>');
                var spaceTd = '<td>';
                for (var j = 0; j < list[i].spaceInfo.length; j++) {
                    spaceTd += ' [';
                    spaceTd +=  '￥'+list[i].spaceInfo[j].money;
                    spaceTd += '] '
                    spaceTd +=  list[i].spaceInfo[j].spaceCode;
                    spaceTd +=  ' (' + list[i].spaceInfo[j].endTime + ')';
                    spaceTd += '<br />';
                }
                spaceTd += '</td>';
                tr.append(spaceTd);
                tr.append('<td>'+list[i].money+'</td>');
                total += list[i].money;
                tbody.append(tr);
            }
            $this.$parent.find('.total-price span').html(total);
        }, false);
    },
    renewal : function () {
        var $this = this;
        if (confirm('是否批量续费？此操作不可逆')) {
            var month = this.$parent.find('#month').val();
            var dataList = [];
            this.$parent.find('#data-list table tbody tr').each(function(inx, itm) {
                var ownerCode = $(itm).find('td:eq(1)').attr('data-value');
                dataList.push(ownerCode);
            });
            var dataT = {
                'month':month,
                'list':dataList
            };
            JsonpAjax(baseApiPath + 'owner_info/renewal', JSON.stringify(dataT), 'json', function(res) {
                $this.$parent.find('#result-page').fadeIn('fast');
                $this.$parent.find('#calc-page').fadeOut('fast');
                var list = res;
                if (isEmpty(list)) {
                    notification({'title':'错误', 'message':'数据出现异常！请联系管理员'},'error');
                    return;
                }
                var tbody = $this.$parent.find('#result-page table tbody');
                tbody.html('');
                for (var i = 0; i < list.length; i++) {
                    var tr = $('<tr></tr>');
                    tr.append('<td>'+list[i].ownerName + ' ('+list[i].ownerCode+')</td>');
                    var tdStr = '<td>';
                    for (var j = 0; j < list[i].spaceList.length; j++) {
                        tdStr += '剩余：['+list[i].spaceList[j].timeout+']天 ' + list[i].spaceList[j].spaceCode + '<br />';
                    }
                    tdStr += '</td>';
                    tr.append(tdStr);
                    tr.append('<td><span style="color:#268600">成功</span></td>')
                    tbody.append(tr);
                }
            }, false);
        }
    },
    finish : function () {
        closeWindow();
        ownerManager.findData();
    }
}
